home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / MacApp Documentation / MacApp AppleLink Messages / MacApp.Tech$ 12⁄8⁄89 / 0175-Failures & Initializ-Dec89 < prev    next >
Encoding:
Text File  |  1989-12-08  |  2.9 KB  |  102 lines  |  [TEXT/GEOL]

  1. Item    7505402                         7-Dec-89        06:21
  2.  
  3. From:   D1950                           CSG, Don Phillips,PRT
  4.  
  5. To:     MACAPP.TECH$                    MacApp Technical
  6.  
  7. Sub:    Failures & Initialization
  8.  
  9. How do I prepare TTObject1.IObject1's failure handler for the situation where
  10. its reference to Object2 is invalid because TTObject2.IObject2's failure
  11. handler has already freed Object2?
  12.  
  13. From MacApp Cookbook: 'Handling errors when creating and initializing objects':
  14.  
  15.     'The failure handler should do any necessary cleanup, and then call Free.'
  16.  
  17.     'When you call the initialization method of an object and it fails, your
  18.     reference to the object may become invalid. If the code calling the
  19.     initialization method has a failure handler, the handler must be prepared
  20.     for this situation.'
  21.  
  22. How do I prepare my failure handler for this situation?
  23.  
  24. It seems to me that a Failure Handler should not free the object itself, but
  25. should leave this to the calling method's Failure Handler???
  26.  
  27. Using the example shown below, if there is a failure in IObject2, its failure
  28. handler will be executed and the object itself will be freed. When IObject1's
  29. failure handler is called next, it will attempt to free an object (tmpObjRef)
  30. which has already been freed. Error!
  31.  
  32. Thanks for any explanations,
  33. Jo-Anne Droogh
  34.  
  35.  
  36. {*** Type Declarations ***}
  37. TObject1 = Object( TObject3 )
  38.     ffObjRef : TObject2;
  39.     PROCEDURE TObject1.IObject1;
  40.  
  41. TObject2 = Object( TObject3 )
  42.     PROCEDURE TObject2.IObject2;
  43.  
  44. { *** Implementation *** }
  45. PROCEDURE TObject1.IObject1;
  46.     VAR
  47.         tmpObjRef   : TObject2;
  48.            fi  : FailInfo;
  49.  
  50.        PROCEDURE LocalFailureHandler( error : OSErr; message : LONGINT );
  51.     { According to the cookbook, this should cleanup and call Free }
  52.  
  53.        BEGIN
  54.         { ... other cleanup ... }
  55.         FreeIfObject( tmpObjRef );
  56.         SELF.Free;
  57.     END;
  58.  
  59. BEGIN
  60.     { First, initialize variables that Free needs to operate successfully. }
  61.     SELF.ffObjRef := NIL;
  62.     tmpObjRef := NIL;
  63.  
  64.     { Next, call the immediate ancestor's initialization method, if any. }
  65.     INHERITED IObject3;
  66.  
  67.     { Since I do some initialization that can fail, set up a failure handler,
  68.         do the initialization, then remove the failure handler. }
  69.    CatchFailures( fi, LocalFailureHandler );
  70.  
  71.     { ... other code that can fail ... }
  72.  
  73.     New( tmpObjRef );
  74.     FailNIL( tmpObjRef );
  75.     tmpObjRef.IObject2;
  76.  
  77.     Success( fi );
  78.  
  79.     SELF.ffObjRef := tmpObjRef;
  80. END;
  81.  
  82. PROCEDURE TObject2.IObject2;
  83.  
  84.     VAR
  85.            fi            : FailInfo;
  86.  
  87.        PROCEDURE LocalFailureHandler( error : OSErr; message : LONGINT );
  88.     { According to the cookbook, this should cleanup and call Free }
  89.  
  90.        BEGIN
  91.         SELF.Free;
  92.     END;
  93.  
  94. BEGIN
  95.     { ... other stuff ... }
  96.     CatchFailures( fi, LocalFailureHandler );
  97.    { ... stuff that can fail ... }
  98.     Success( fi );
  99. END;
  100.  
  101.  
  102.